"""
积分（integral）相关用例
"""
import unittest
from common.Base import open_op_browser
from Page.user_welcome_page import path, welcome_url, WelcomePage
from common.databaseTool import DatabaseTool


class IntegralCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # 大前提  打开数据库
        cls.db = DatabaseTool("168168", "ecshop_db", port=3308)

    def setUp(self):
        # 预置条件 使用加载项 进入指定页面
        driver = open_op_browser(path)
        self.lp = WelcomePage(driver)
        self.lp.open_url(welcome_url)

    def tearDown(self):
        self.lp.quit()

    def test_integral_001(self):  # 验证已登录用户成功进入 用户欢迎页
        try:
            self.assertIn("欢迎您回到", self.lp.welcome_text(), msg="用例执行失败")
            print(self.lp.welcome_text())
        except Exception as e:
            raise Exception("用例执行失败")

    def test_integral_002(self):  # 验证已登录新用户的积分情况
        try:
            sql = "select pay_points from ecs_users where user_name='admin123'"
            sq = str(self.db.fetchOne(sql)["pay_points"])
            self.assertIn(sq + "积分", self.lp.user_integral_text(), msg="用例执行失败")
            print(self.lp.user_integral_text())
        except Exception as e:
            raise Exception("用例执行失败")

    def test_integral_003(self):  # 验证有过购物的用户的积分情况
        try:
            sql1 = "update ecs_users set pay_points='1000' rank_points='1000' where user_name='admin123'"  # 在数据库中修改会员积分为1000
            self.db.execute_one(sql1)
            sql = "select pay_points from ecs_users where user_name='admin123'"  # 查询数据库中的积分
            sq = str(self.db.fetchOne(sql)["pay_points"])  # 获取 数据库中 积分
            self.lp.driver.refresh()  # 刷新页面
            self.assertIn(sq + "积分", self.lp.user_integral_text(), msg="用例执行失败")
            print(self.lp.user_integral_text())
            sql2 = "update ecs_users set pay_points='0' where user_name='admin123'"  # 在数据库中修改会员积分为0
            self.db.execute_one(sql2)

        except Exception as e:
            raise Exception("用例执行失败")

    def test_integral_004(self):  # 验证有过购物的用户的等级积分情况
        try:
            sql1 = "update ecs_users set rank_points='1000' pay_points='1000' where user_name='admin123'"  # 在数据库中修改会员积分为1000
            self.db.execute_one(sql1)
            sql = "select rank_points from ecs_users where user_name='admin123'"
            sq = int(str(self.db.fetchOne(sql)["rank_points"]))
            ss = 10000 - sq
            # print(ss)
            self.assertIn(f"您还差 {ss} 积分达到", self.lp.grade_integral_text(), msg="用例执行失败")
            print(self.lp.grade_integral_text())
            sql2 = "update ecs_users set rank_points='0' where user_name='admin123'"  # 在数据库中修改会员积分为0
            self.db.execute_one(sql2)
        except Exception as e:
            raise Exception("用例执行失败")

    def test_integral_005(self):  # 验证已登录新用户的等级积分情况
        try:
            sql1 = "update ecs_users set rank_points='0' pay_points='0' where user_name='admin123'"  # 在数据库中修改会员积分为1000
            self.db.execute_one(sql1)
            self.assertIn("您还差 10000 积分达到 vip", self.lp.grade_integral_text(), msg="用例执行失败")
            print(self.lp.grade_integral_text())
        except Exception as e:
            raise Exception("用例执行失败")


if __name__ == '__main__':
    unittest.main()
